// pages/integral-goods/integral-goods.js
import mode from "../../utils/mode.js"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
    data: {
        
        showOK:false,
        temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg'],
        integral_id:'',
        isRequest:false,
        userMessage:{},
        level: '',

    },
    handleClickRouter () {
        wx.navigateTo({
            url: "/pages/interests-rule/interests-rule"
        })
    },
    handleClickRegister () {
        wx.navigateTo({
            url: "/pages/register/register"
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    handleClickGoodsDetails (e) {  //兑换商品详情
        let type = e.currentTarget.dataset.type
        let goodId = e.currentTarget.dataset.id
        var that = this
        if (!that.data.userMessage) {
            wx.navigateTo({
                url: "/pages/register/register"
            })
        }else{ 
            wx.navigateTo({
                url: "/pages/goods-details/goods-details?id="+goodId+"&type=" + type
            })
        }
    },
    handleClickGoodsDetailsNow(e){   //立即兑换
        var that = this
        if(that.data.isRequest==true)  return
        that.setData({
          isRequest:true
        })
        let useScore = e.currentTarget.dataset.score
        let good_id = e.currentTarget.dataset.id
        let good_type = e.currentTarget.dataset.type
        let good = that.data.first
        if (!that.data.userMessage) {
            that.setData({
                isRequest:false
              })
            wx.navigateTo({
                url: "/pages/register/register"
            })
        }else if(good.coupon_user_type_limit>0){
            if(good.coupon_user_type_limit==1&&that.data.userMessage.is_member!=1){
                that.setData({
                    isRequest:false
                 })
                 Dialog.alert({
                     message: '您还未绑定会员 \n 无法兑换',
                 }).then(() => {
                 });
            }else if(good.coupon_user_type_limit==2&&that.data.userMessage.is_newUser!=1){
                that.setData({
                    isRequest:false
                 })
                 Dialog.alert({
                     message: '仅限于新用户 \n 无法兑换',
                 }).then(() => {
                 });
            }else{
                that.tochange(good_type,good_id)   //立即兑换
            }
        }else if(good.status==0){
            that.setData({
              isRequest:false
            })
            wx.hideLoading()
            let title = good_type==1?'此商品已下架':'此优惠券已下架'
            Dialog.alert({
              message: title,
          }).then(() => {
          });
          }
        //   else if(good.is_limit==1&&good.surplus_num<1){
        //     that.setData({
        //       isRequest:false
        //     })
        //     wx.hideLoading()
        //     Dialog.alert({
        //       message: '您已超出产品限额， \n 无法兑换',
        //     }).then(() => {
        //     });
        //   } 
          else if(useScore>that.data.userMessage.score) {
            that.setData({
                isRequest:false
              })
            Dialog.alert({
                message: '积分不足 \n 无法兑换',
              }).then(() => {
              });
        }else{ 
            if(good_type==1){
                that.setData({
                    isRequest:false
                  })
                wx.navigateTo({
                    url: "/pages/settlement/settlement?goods_id="+good_id+"&number=1&orders_type="+good_type
                })
            }else{
                that.tochange(good_type,good_id)   //立即兑换
            }
        }
    },
    toMyScore () {
        if(this.data.userMessage&&this.data.userMessage.score>0){
            wx.navigateTo({
                url: '/pages/record/record',
            })
        }
    },
    tochange(good_type,good_id){    //立即兑换优惠券
        var that = this
        wx.$api.createIntegralOrder({
                user_id: that.data.userMessage.id,
                number:1,
                orders_type:good_type,     //商品类型     1商品    2 优惠券
                integral_good_id:good_type==1?good_id:'',
                integral_coupon_id:good_type==2?good_id:'',
                notes:''
            }).then(res=>{
                if(res.code==1){
                    that.setData({
                        integral_id:res.data.integral_id,
                        // showOK:true,
                        // isRequest: false
                    })
                    that.getUserInfo()
                }else{
                    that.setData({
                       isRequest:false
                    })
                    Dialog.alert({
                       message: res.msg,
                    }).then(() => {});
                }
        })
    },
    //获取用用户的积分
    getUserInfo () {
      let that = this
      var userInfo = wx.getStorageSync('uinfo')
      mode.getUserInfo(userInfo.user.id).then(item => {
        userInfo.user = item.data
        wx.setStorage({key:'uinfo',data:userInfo})
        that.setData({
            userMessage: item.data,
            isRequest: false
        })
        setTimeout(() => {
            wx.navigateTo({
                url: '/pages/exchangeDetail/exchangeDetail?id='+that.data.integral_id    //integral_id    兑换详情     orders_id 订单详情
            })
        }, 1200)
      })
  },
  
  setInfo(){// 订阅消息
    let that = this
    this.setData({
        showOK:false
    })
    let shareData={
      user_id:that.data.userMessage.id,
      type:3,
      other_type:'',
    }
    wx.requestSubscribeMessage({
        tmplIds: that.data.temIds,
        success (res) {
            if(res[that.data.temIds[0]]=='accept'){//同意
               shareData.other_type='1'
            }
            that.sendSetYue(shareData)
        },
        fail(error){
            setTimeout(() => {
                wx.navigateTo({
                    url: '/pages/exchangeDetail/exchangeDetail?id='+that.data.integral_id    //integral_id    兑换详情     orders_id 订单详情
                })
            }, 1200)
          console.log(error,'123失败');
        }
      })
  },
  sendSetYue(data){
      let that = this
    wx.$api.sendTemplate(data).then(res=>{
            if(res.code>0){
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
            setTimeout(() => {
                wx.navigateTo({
                    url: '/pages/exchangeDetail/exchangeDetail?id='+that.data.integral_id    //integral_id    兑换详情     orders_id 订单详情
                })
            }, 1200)
    })
},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var uinfo = wx.getStorageSync('uinfo')
        if (uinfo && uinfo.user) {
            let name = uinfo.user.grade==1?'银钻会员':uinfo.user.grade==2?'金钻会员':uinfo.user.grade==3?'黑钻会员':''
            this.setData({
                id: uinfo.user.id,
                userMessage: uinfo.user,
                level:name
            })
        }
        this.getIntegralGoods()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    getIntegralGoods () {
        let that = this
        wx.$api.integralGoodsLists({
                user_id:that.data.userMessage.id
            }).then(item=>{
                that.setData({
                    first: item.data[0],
                    integralList: item.data
                })
        })
    },
})